package hzst.android.form.info;

import hzst.android.form.ViewInfoProcessor;
import hzst.android.util.L;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * 表格控件{@link #FMFormView}绑定数据
 * @author wt
 *
 */
public class FormViewInfo extends BaseViewInfo{
	private static final long serialVersionUID = 1L;
	
	private List<List<FormSubmitValue>> formValues;
	
	public FormViewInfo(String submitInfo) {
		setSubmitInfo(submitInfo);
		parseSubmitInfo();
	}

	@Override
	public void parseSubmitInfo() {
		super.parseSubmitInfo();
		
		formValues = new ArrayList<List<FormSubmitValue>>();
		String temp = "";
		List<FormSubmitValue> list = null;
		try {
			JSONArray array = new JSONArray(submitInfo);
			for (int i = 0; i < array.length(); i++) {
				JSONObject obj = array.getJSONObject(i);
				FormSubmitValue value = new FormSubmitValue();
				value.setFieldName(submitValues.get(i).getFieldName());
				value.setValue(submitValues.get(i).getValue());
				value.setValueType(submitValues.get(i).getValueType());
				value.setBroadName(ViewInfoProcessor.parseJsonElement(obj, "broadName"));
				value.setItemName(ViewInfoProcessor.parseJsonElement(obj, "itemName"));
				submitValues.add(value);
				
				/*
				 * 把相同broadName的
				 */
				if("".equals(temp) || !value.getBroadName().equals(temp)){
					if(list != null){
						formValues.add(list);
					}
					list = new ArrayList<FormViewInfo.FormSubmitValue>();
					temp = value.getBroadName();
				}
				list.add((FormSubmitValue) value);
			}
		} catch (JSONException e) {
			L.showLogInfo(L.TAG_EXCEPTION, e.toString());
		}

	}

	@Override
	public void setSubmitInfo(String submitInfo) {
		this.submitInfo = submitInfo;
	}

	public List<List<FormSubmitValue>> getFormValues() {
		return formValues;
	}

	public void setFormValues(List<List<FormSubmitValue>> formValues) {
		this.formValues = formValues;
	}
	
	public class FormSubmitValue extends SubmitValue implements Serializable{
		private static final long serialVersionUID = 1L;
		
		private String broadName = "";
		private String itemName = "";
		
		public String getBroadName() {
			return broadName;
		}
		public void setBroadName(String broadName) {
			this.broadName = broadName;
		}
		public String getItemName() {
			return itemName;
		}
		public void setItemName(String itemName) {
			this.itemName = itemName;
		}
	}
	
}
